<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->
    <van-cell title="头像" is-link @click="$refs.img.click()">
      <van-image class="avatar" fit="cover" round :src="user.photo" />
    </van-cell>
    <input type="file" hidden ref="img" accept="image/png" @change="onFiles" />
    <van-cell
      title="昵称"
      @click="isShowUpdateName = true"
      :value="user.name"
      is-link
    />
    <van-cell
      title="性别"
      @click="isShowUpdateGender = true"
      :value="user.gender ? '女' : '男'"
      is-link
    />
    <van-cell
      title="生日"
      @click="isShowUpdateBirthdy = true"
      :value="user.birthday || '1977-00-00'"
      is-link
    />
    <van-popup
      v-model="isShowUpdateName"
      style="height: 100%"
      position="bottom"
    >
      <updateName
        v-model="user.name"
        v-if="isShowUpdateName"
        @close="isShowUpdateName = false"
      />
    </van-popup>
    <van-popup v-model="isShowUpdateGender" position="bottom">
      <update-gender
        v-if="isShowUpdateGender"
        v-model="user.gender"
        @close="isShowUpdateGender = false"
      />
    </van-popup>
    <van-popup v-model="isShowUpdateBirthdy" position="bottom">
      <update-birthdy
        @close="isShowUpdateBirthdy = false"
        v-if="isShowUpdateBirthdy"
        v-model="user.birthday"
      />
    </van-popup>
    <van-popup v-model="isShowUpdateImg" style="height: 100%" position="bottom">
      <update-img
        v-if="isShowUpdateImg"
        @update-avatar="user.photo = $event"
        :img="img"
        @close="imgClose"
      />
    </van-popup>
  </div>
</template>
<script>
import { getUserProfile } from '@/api/user.js'
import updateName from './components/update-name.vue'
import updateGender from './components/update-gender.vue'
import updateBirthdy from './components/update-birthdy.vue'
import updateImg from './components/update-img'

export default {
  created() {
    this.loadProfile()
  },
  computed: {},
  components: { updateName, updateGender, updateBirthdy, updateImg },
  data() {
    return {
      user: {},
      isShowUpdateName: false,
      isShowUpdateGender: false,
      isShowUpdateBirthdy: false,
      isShowUpdateImg: false,
      img: ''
    }
  },
  methods: {
    imgClose() {
      this.isShowUpdateImg = false
      this.$refs.value = ''
    },
    onFiles(e) {
      const file = e.target.files[0]
      console.log(e.target.value)
      this.img = window.URL.createObjectURL(file)
      this.isShowUpdateImg = true
      e.target.value = ''
    },
    async loadProfile() {
      try {
        const res = await getUserProfile()
        this.user = res.data.data
      } catch (err) {
        this.$toast('获取数据失败')
      }
    }
  }
}
</script>
<style lang="less" scoped>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background: #f5f7f9;
  }
}
</style>
